<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Forms\GroupForm;
use App\Modules\Shop\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Modules\User\Models\Admin;

class GroupController extends Controller
{

    public function index(Request $request)
    {
        $this->authorize('group');
        $name = $request->input('name', '');
        $data = Group::query()
            ->when(!empty($name), function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . "%");
            })
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        
        return view('group.index', compact('data', 'name'));
    }

    public function create(Request $request)
    {
        $this->authorize('group-add');

        $form = $this->form(GroupForm::class, [
            'url'    => route('admin.group.store'),
            'method' => 'POST',
        ]);
       
        return view('group.form', compact('form', 'shopId'));
    }

    public function store(Request $request)
    {
        $this->authorize('group-add');
        $form = $this->form(GroupForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['admin_id'] = Auth::user()->id;

        Group::query()->create($data);
        return redirect()->route('admin.group.index');
    }

    public function edit(Group $group, Request $request)
    {
        $this->authorize('group-edit');
        
        $form = $this->form(GroupForm::class, [
            'url' => route('admin.group.update', ['group' => $group]),
            'method' => 'PUT',
            'model' => $group
        ]);
        return view('group.form', compact('form'));
    }

    public function update(Group $group, Request $request)
    {
        $this->authorize('group-edit');
        $form = $this->form(GroupForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['admin_id'] = Auth::user()->id;
        $group->update($data);
        return redirect()->route('admin.group.index');
    }

    public function destroy(Group $group)
    {
        $this->authorize('group-del');
        $group->delete();
        return redirect()->route('admin.group.index');
    }

    public function getgroup(Request $request)
    {
        $term = $request->input('term');

        $query = Group::query();

        if (!empty($term)) {
            $query->where('name', 'like', "%".$term."%");
        }
        $re = $query->get()->pluck('name', 'id')->toArray();
        array_unshift($re, '无');
        return $re;
    }

    public function changeSort(Request $request)
    {
        $id   = $request->input('id');
        $sort = $request->input('sort');
        Group::query()
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return redirect()->back();
    }
}
